#	extract features from the original RSSI log files.
#	

#	define a struct that represent high RSSI sequence, attributes contains:
#	(1) sequence id; (1) average RSSI; (2) range(RSSI)  (3) successive number; 


#	define a struct that represent devices.
#	(1) device id; (2) interval length; (3) sequence id array.




opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";
open Output, ">$ARGV[0]\\total.feature" or die "$ARGV[0]\\total.feature";
$fileNum = 0;
	$temp = 0;
	#types of signal source
	$zigbee  = "ZigBee";
	$bluetooth = "BT";
	$microwave ="MO";
	$wifi = "WiFi";

$indexHigh = 0;		#index of high sequence

while ($file = readdir $dir) {
	$file_path = "$ARGV[0]\\$file";
	$firstline = 1;			# whether the first or not. First is the start time and end time.
	@samples[60000];		
	$sampleNum = 0;		#number of samples in this sampling
	$label = -1;			#indicate the types of signal source, from 0-3.

	$noise = 0;			#nosie floor of sampling
	$highSucNum = 0;		#number of successive high RSSI
	$sucNum = 0;			#number of successive concerned RSSI, set to 0 after using.
	$startHigh = -1;		#start index of high sequence
	$endHigh = -1;			#end index of high sequence
	$largest = 0;			#largest high value
	$smallest = 0;			#smallest high value
	$rangeHigh = 0;		# range of high RSSI
	$lastEnd = 0;			# last index of the high RSSI, to measure packet interval.
	$interval  = -1;			# quiet length, from last high to this start high.
	$i = 0;
	$averageRSSI = 0;
	$startLow = -1;
	$endLow = -1;
	$lowSucNum = 0;

print ("$file_path  \n $file\n");
	$logfile = "\.log";
	#get input samples
	if($file =~ /$logfile/) {
	open fin, "<$file_path" or die "can not open file $file_path!\n";
	$fileNum++;
	while (<fin>) {
		chomp;
		@info = split;
			
		if($firstline==1) {
			$firstline = 0;
			next;
		}
		$samples[$sampleNum++] = $info[1];
	}
	close fin;
	}
	else {
		print("No log files found!\n");
		next;
	}

	$feidian = "feidian";
	#get the source type
	if($file =~ /$zigbee/) {
		$label = 0;
	}
	if($file =~ /$bluetooth/) {
		$label = 1;
	}
	if($file =~ /$microwave/ && !($file =~ /$feidian/) ) {
		$label = 2;
	}
	if($file =~ /$wifi/) {
		$label = 3;
	}
	if($label==-1){
		next;
	}
print ("label: $label\n");
	#get the noise floor
	#criterion: 	abs(neighboring samples)<3dBm  &&  -100 < samples < -90
	#			&&  number of these sample sequence > 50 ?? ( it depends on how busy the channel is)
	for($i=1;$i<$sampleNum;$i++){
		if($samples[$i]< -90 && $samples[$i] >-100){
			if( abs($samples[$i] - $samples[$i-1]) <  3) {
				$sucNum++;
			}
			else {
				$sucNum = 0;
			}
		}
		if($sucNum>=50) {
			$noise = $samples[$i-10];
			$sucNum = 0;				#set 0 after using.
			last;
		}
	}
print ("noise floor:$noise\n");
	$largest = $noise;

	#get the information of successive sequence 
	#algorithm:	samples[i..i+4] > (noise+5dBm)  ==> high samples
	#			count the number.
	for($i=0;$i<$sampleNum;$i++) {

		#get the low sequence information
		if($samples[$i] < ($noise -5)) {
			$startLow = $i;
			#print ("****************$i,$samples[$i]\n");
		}
		if($startLow !=-1 && abs($samples[$i+1] -$noise) < 3 ) {
			$endLow = $i;
		}
		if($startLow!=-1 && $endLow !=-1) {
			$lowSucNum = $endLow-$startLow;
			if($lowSucNum>0){
				#print "***************$label \t$startLow \t$endLow \t$lowSucNum\n";
			}
			$startLow = -1;
			$endLow = -1;
			$lowSucNum = 0;
		}
		else{
			if(($i-$startLow)>=2){
				$startLow ==-1;
				$endLow == -1;
			}
		}

		#get high sequence information
		if( $samples[$i] > ($noise + 10) ) {
			if($label!=2 && $startHigh==-1){$startHigh = $i;}
			if($label==2 && $startHigh==-1 && $samples[$i] > ($noise + 15) ){$startHigh = $i;}
		}
		
		else {
			if($startHigh==-1) {
				next;
			}
			# three low separated
			#if(abs($samples[$i+1] -$samples[$i]) < 3 && abs($samples[$i+2] -$samples[$i+1]) < 3 ) {
			# two low separated
			if(abs($samples[$i+1] -$samples[$i]) < 3 ) {
				$endHigh = $i;
			}
		}
		if($startHigh != -1 && $endHigh == -1) {
			if($samples[$i]> $largest) {
				$largest = $samples[$i];
			}
			#if($label == 0 && !( abs($samples[$i+1] -$samples[$i]) < 5 && abs($samples[$i+2] -$samples[$i+1]) < 5 && $samples[$i]> ($noise+15))) { next;}
			if(($label == 1 || $label == 0)&& !( $samples[$i] > ($noise+15) )) {next;}
			if($label == 2 && !( $samples[$i] > ($noise+10) )){next;}
			if($label == 3 && !( $samples[$i] > ($noise+20) )){next;}
			$averageRSSI += $samples[$i];
			$sucNum++;
			#print ("$averageRSSI $sucNum\n");
			if($samples[$i] < $smallest){
				if($label == 0 && abs($samples[$i+1] -$samples[$i]) < 5 && abs($samples[$i+2] -$samples[$i+1]) < 5 && $samples[$i]> ($noise+15)) {
					$smallest = $samples[$i];
				}
				if($label == 1 && $samples[$i] > ($noise+15) ) {
					$smallest  = $samples[$i];
				}
				if($label == 2 && $samples[$i] > ($noise+10) ) {
					$smallest  = $samples[$i];
				}
				if($label == 3 && $samples[$i] > ($noise+20) ) {
					$smallest  = $samples[$i];
				}
			}
			next;
		}
		#get this sequence statistics
		if($averageRSSI != 0) {
			if($lastEnd != 0){ 
				$interval  = $startHigh -$lastEnd;
			}
			$lastEnd = $endHigh;
			$highSucNum = $endHigh -$startHigh +1;
			$rangeHigh = $largest -$smallest;
			$averageRSSI = int( $averageRSSI / $sucNum);
			if($label == 0 && $highSucNum <150 && $highSucNum >13){
				print "$indexHigh \t$label \t$startHigh \t$endHigh \t$highSucNum \t$largest \t$smallest \t$rangeHigh \t$averageRSSI \t$sucNum \t$interval\n";
				print Output "$indexHigh \t$label \t$startHigh \t$endHigh \t$highSucNum \t$largest \t$smallest \t$rangeHigh \t$averageRSSI \t$sucNum \t$interval\n";
				$indexHigh++;
			}
			if($label!=0){
				print "$indexHigh \t$label \t$startHigh \t$endHigh \t$highSucNum \t$largest \t$smallest \t$rangeHigh \t$averageRSSI \t$sucNum \t$interval\n";
				print Output "$indexHigh \t$label \t$startHigh \t$endHigh \t$highSucNum \t$largest \t$smallest \t$rangeHigh \t$averageRSSI \t$sucNum \t$interval\n";
				$indexHigh++;				
			}
			# analyze the range information, for zigbee, larger than 5, for others, samller than 5.
			if($label==0 && $rangeHigh>5){$temp++;}
			if($label!=0 && ($rangeHigh<5 ||$highSucNum <11 )){$temp++;}
		}

		#set default values for next detection.
		$largest = $noise;
		$smallest = 0;
		$startHigh = -1;
		$endHigh = -1;
		$rangeHigh  = 0;
		$highSucNum = 0;
		$sucNum = 0;
		$averageRSSI = 0;
	}


	#get the number of singal interval.
	#algorithm:	when find high sequences, save its START time to the arrary indexed by RSSI values.
	#			the array can have -100,0, every 5dBm has one row. This is used for identifying the source of signal.
	#			Not the type of source signal, it is used for distinguish devices to get the interval realted to one device.
	#TODO: right now, it is interval between successive high sequence without distinguishing devices
	#		We need to get the interval between two successive high sequence from the SAME ONE device.


	#get the fluctuation of high sequences
	#criterion:	range(RSSI) > 5 dBm (?), it fluctuates. Otherwise, not.


	#TODO: get the successive low RSSI length
	#criterion: 5dbm smaller than noise, and if number of successive length > 5, ==> low noise singal exists ==> MO
	print ("$noise\n $file\n");
	
}
# analyze the range information, for zigbee, larger than 5, for others, samller than 5.
print "Range> or <5: $temp\n";
close Output;

$cmd = "coffee";
$file = $ARGV[0];
print ("$file\n");
if($file =~ /$bluetooth/) {
	$cmd = "copy /Y ..\\controlLog\\BT\\synchronized\\total.feature ..\\controlLog\\rawTotalFeature\\BTsynchronized.feature";
}
if($file =~ /$zigbee/) {
	$cmd = "copy /Y ..\\controlLog\\ZigBee\\total.feature ..\\controlLog\\rawTotalFeature\\ZigBee.feature";
}
if($file =~ /$microwave/) {
	$cmd = "copy /Y ..\\controlLog\\MO\\total.feature ..\\controlLog\\rawTotalFeature\\MO.feature";
}
if($file =~ /$wifi/) {
	$cmd = "copy /Y ..\\controlLog\\WiFi_11n\\total.feature ..\\controlLog\\rawTotalFeature\\WiFi_11n.feature";
}

print("$cmd\n");
system("$cmd\n");
